package com.and.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.and.analyzer.Analyzers;
import com.and.bean.Participle;
import com.and.bean.ParticipleCategory;
import com.and.bean.Suggestion;
import com.and.dao.Dao;
import com.and.dao.QueryResult;

@Controller
public class SuggestionAction {

	@Resource
	protected Dao dao;

	private Integer id;
	private String suggest;
	private ParticipleCategory participleCategory;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getSuggest() {
		return suggest;
	}

	public void setSuggest(String suggest) {
		this.suggest = suggest;
	}

	public ParticipleCategory getParticipleCategory() {
		return participleCategory;
	}

	public void setParticipleCategory(ParticipleCategory participleCategory) {
		this.participleCategory = participleCategory;
	}

	public String jumpPass() {
		return "jumpPass";
	}
	
	public String save() {
		try {
			List<String> listAnalyzer = Analyzers.testAnalyzer(
					new IKAnalyzer(), suggest.trim());
			boolean boo = true;
			for (String string : listAnalyzer) {
				QueryResult<Participle> query = dao.getScrollData(
						Participle.class, "speech = ?1",
						new Object[] { string });
				if (query.getGetall().size() == 1) {
					for (Participle mParticiple : query.getGetall()) {
						Suggestion mSuggestion = new Suggestion();
						mSuggestion.setSuggest(suggest.trim());
						mSuggestion.setParticipleCategory(mParticiple
								.getParticipleCategory());
						mSuggestion.setParticiple(mParticiple);
						dao.save(mSuggestion);
						mParticiple.setAmount(mParticiple.getAmount() + 1);
						dao.update(mParticiple);
						boo = false;
						return "save";
					}
				}
			}
			if (boo) {
//				Suggestion mSuggestion = new Suggestion();
//				mSuggestion.setSuggest(suggest.trim());

				QueryResult<ParticipleCategory> query = dao.getScrollData(
						ParticipleCategory.class, "category = ?1",
						new Object[] { "其它" });
				if (query.getGetall().size() == 1) {
					for (ParticipleCategory mParticipleCategory : query
							.getGetall()) {
//						mSuggestion.setParticipleCategory(dao.find(
//								ParticipleCategory.class,
//								mParticipleCategory.getId()));
//						dao.save(mSuggestion);

						// 记录分词
						setAnalyzers(suggest.trim(),
								mParticipleCategory.getId());
					}
					
					for (String string : listAnalyzer) {
						QueryResult<Participle> query1 = dao.getScrollData(
								Participle.class, "speech = ?1",
								new Object[] { string });
						if (query1.getGetall().size() == 1) {
							for (Participle mParticiple : query1.getGetall()) {
								Suggestion mSuggestion = new Suggestion();
								mSuggestion.setSuggest(suggest.trim());
								mSuggestion.setParticipleCategory(mParticiple
										.getParticipleCategory());
								mSuggestion.setParticiple(mParticiple);
								dao.save(mSuggestion);
								mParticiple.setAmount(mParticiple.getAmount() + 1);
								dao.update(mParticiple);
								return "save";
							}
						}
					}
					
					
				} else if (query.getGetall().size() < 1) {
					ParticipleCategory mParticipleCategory = new ParticipleCategory();
					mParticipleCategory.setCategory("其它");
					dao.save(mParticipleCategory);

					QueryResult<ParticipleCategory> query1 = dao.getScrollData(
							ParticipleCategory.class, "category = ?1",
							new Object[] { "其它" });
					if (query1.getGetall().size() == 1) {
						for (ParticipleCategory mParticipleCategory1 : query1
								.getGetall()) {
							// 记录分词
							setAnalyzers(suggest.trim(),
									mParticipleCategory1.getId());
						}
						
						for (String string : listAnalyzer) {
							QueryResult<Participle> query2 = dao.getScrollData(
									Participle.class, "speech = ?1",
									new Object[] { string });
							if (query2.getGetall().size() == 1) {
								for (Participle mParticiple : query2.getGetall()) {
									Suggestion mSuggestion = new Suggestion();
									mSuggestion.setSuggest(suggest.trim());
									mSuggestion.setParticipleCategory(mParticiple
											.getParticipleCategory());
									mSuggestion.setParticiple(mParticiple);
									dao.save(mSuggestion);
									mParticiple.setAmount(mParticiple.getAmount() + 1);
									dao.update(mParticiple);
									return "save";
								}
							}
						}
						
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "save";
	}
	
	public String findAllSuggestion(){
		ParticipleCategory mParticipleCategory = dao.find(ParticipleCategory.class, id);
		QueryResult<Suggestion> query = dao.getScrollData(
				Suggestion.class, "participleCategory = ?1",
				new Object[] { mParticipleCategory });
		
		ServletActionContext.getRequest().setAttribute("orgs", query);
		ServletActionContext.getRequest().setAttribute("category", mParticipleCategory.getCategory());
		return "findAllSuggestion";
	}
	

	private void setAnalyzers(String text, int mid) throws Exception {
		List<String> listAnalyzer = Analyzers.testAnalyzer(new IKAnalyzer(),
				text);
		for (String string : listAnalyzer) {

			QueryResult<Participle> query = dao.getScrollData(Participle.class,
					"speech = ?1", new Object[] { string.trim() });
			if (query.getGetall().size() == 0) {
				Participle mParticiple = new Participle();
				mParticiple.setSpeech(string.trim());
				mParticiple.setParticipleCategory(dao.find(
						ParticipleCategory.class, mid));
				dao.save(mParticiple);
				System.out.println("-----" + string);
			}
		}
	}

}
